package lium.lbs.lbsim;

import android.os.Bundle;
import android.support.annotation.*;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.lmlibrary.Constants;
import com.lmlibrary.R;
import com.lmlibrary.bean.ResponseBean;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okgo.utils.OkLogger;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import lium.lbs.lbsim.base.BaseParamMap;

import java.io.File;
import java.util.HashMap;

public abstract class OBaseFragment extends Fragment {
    private View contentView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (contentView != null) {
            ViewGroup parent = (ViewGroup) contentView.getParent();
            if (parent != null) {
                parent.removeView(contentView);
            }
            return contentView;
        }
        contentView = inflater.inflate(R.layout.lb_activity_base_content, container, false);
        return contentView;

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }


    public OBaseActivity getBaseActivity() {
        return (OBaseActivity) getActivity();
    }


    public void displayImage(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.icon_gray_rubbish);
        Glide.with(getBaseActivity()).load(url).apply(options).into(imageView);
    }

    public void displayImage(File file, ImageView imageView) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.icon_gray_rubbish);
        Glide.with(getBaseActivity()).load(file).apply(options).into(imageView);
    }

    public void displayImage(String url, @DrawableRes int errorImage, ImageView imageView) {
        RequestOptions options = new RequestOptions();
        options.placeholder(errorImage);
        Glide.with(getBaseActivity())
                .load(url)
                .apply(options)
                .into(imageView);
    }


    public <T> void postData(String url, HashMap paramMap, Callback<T> callback) {
        BaseParamMap m = new BaseParamMap();
        m.putAll(paramMap);
        String paramJson = new Gson().toJson(m);
        OkLogger.e("接口--" + url + "--参数:--" + paramJson + "--body");
        PostRequest postRequest = OkGo.<ResponseBean<T>>post(Constants.Host + url)//
                .tag(this)
                .isMultipart(true); //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
        postRequest.getParams().put(m);
        postRequest.execute(callback);
    }

    public CompositeDisposable mCompositeDisposable;

    /**
     * 添加订阅
     */
    public void addDisposable(Disposable mDisposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(mDisposable);
    }


    /**
     * 取消所有订阅
     */
    public void clearDisposable() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        clearDisposable();
    }

}